var mcMap={};
    // g_carArea.forEach(function(o){
    //     var el = $('#'+o.id+' .window')[0],
    //     // hammertime = new Hammer(el,{}),
    //     mc = new Hammer.Manager(el);
    //     mc.add(new Hammer.Pinch());
    //     mc.add(new Hammer.Pan({ threshold: 0, pointers: 0}));
    //     mc.add(new Hammer.Tap());
    //     mcMap[o.id] = mc;
    // });
var newsilen ={};
newsilen.install = function(o,getinfobyid){
    var spid = 'zone_'+o.id;
    var el = $('#'+spid+' .window')[0],
    // hammertime = new Hammer(el,{}),
    mc = new Hammer.Manager(el);
    mc.add(new Hammer.Pinch());
    mc.add(new Hammer.Pan({ threshold: 0, pointers: 0}));
    mc.add(new Hammer.Tap());
    mcMap[spid] = mc;
    this.chooseCar(o.originalWidth,o.originalHeight,o.defaultWidth,true,spid,getinfobyid);
}
newsilen.chooseCar=function(w,h,drw,isMobile,id,getinfobyid){
var ORIGINAL_WIDTH = w,ORIGINAL_HEIGHT = h,DEFAULT_REAL_WIDTH = drw;
// var windowWidth = isMobile?window.screen.availWidth/(window.devicePixelRatio || 1):1200,
// windowHeight = isMobile?(window.screen.availHeight*0.65/(window.devicePixelRatio || 1)).toFixed():500;
var windowWidth = isMobile?$('body').width():1200,
windowHeight = isMobile?($('body').height()*0.75).toFixed():500;
// console.log(windowWidth)
$('#'+id+' .window').height(windowHeight).width(windowWidth);

var realWidth,realHeight,sct;
realWidth = DEFAULT_REAL_WIDTH;
realHeight = DEFAULT_REAL_WIDTH * ORIGINAL_HEIGHT / ORIGINAL_WIDTH;
$('#'+id+' .real').width(realWidth).height(realHeight);
var MIN_REAL_WIDTH = windowWidth>600?1300:800,MAX_REAL_WIDTH=w;
var thumbWidth = Math.min(windowWidth/2-32,300),
    thumbHeight=thumbWidth*ORIGINAL_HEIGHT / ORIGINAL_WIDTH;
var zoomRate = realWidth/thumbWidth; // 雷达比例
// 设定小放大镜大小
var magnifierWidth = windowWidth/zoomRate;
var magnifierHeight= windowHeight/zoomRate;

// 以大图的中心为坐标原点 x 向右为正，y 向下为正
var dx = 0,dy = 0,speed=windowWidth>600?1:2,
maxX= realWidth - windowWidth,minY=windowHeight-realHeight,
minX = 0,maxY = 0;
var sc=1,minSc = Math.max(
    MIN_REAL_WIDTH / DEFAULT_REAL_WIDTH,
    // Math.sqrt(magnifierWidth*2 / thumbWidth) 
    magnifierWidth/thumbWidth,
    magnifierHeight/thumbHeight
),
maxSc = 1.5*MAX_REAL_WIDTH/DEFAULT_REAL_WIDTH;
function setScope(){
// 重新设置边界
maxX = realWidth*sc/2 + realWidth/2 - windowWidth;
minX = (1-sc)*realWidth/2;
maxY = (sc-1)*realHeight/2;
minY = -(realHeight*sc/2 + realHeight/2 - windowHeight) ;
}
mcMap[id].off('tap').on('tap',function(e){ // 选择车位
    var _this = $(e.target);
    if(_this.is($('.yrg'))){
    	// sweetAlert('','该车位已被选','error');
        getinfobyid(_this.attr('tip'),'yrg');
    }else if(_this.is($('.rgz'))){
    	sweetAlert('','该车位正在认购中，已被锁定！','error');
    }else if(_this.is($('.drg'))){
    	getinfobyid(_this.attr('tip'));
    }
});
var scaled=0;
mcMap[id].off('panstart').on('panstart',function(e){
windowWidth<=600 && showThumb();
});
mcMap[id].off('panmove').on("panmove", function (e) {
if(scaled) {
return;
}
trans(dx + speed*e.deltaX,dy + speed*e.deltaY);
});
mcMap[id].off('panend').on("panend", function (e) { // 保留最终状态
if(scaled) {
scaled=0;
return;
}
dx = dx + speed*e.deltaX;
dy = dy + speed*e.deltaY;

var dxAlpha = (1-sc) * (realWidth - windowWidth)/2 + dx * sc;
var dyAlpha = (sc -1) * (realHeight - windowHeight)/2 + dy * sc;

// 边界限制
dxAlpha = Math.max(minX, Math.min(maxX,dxAlpha) );
dyAlpha = Math.max(minY, Math.min(maxY,dyAlpha) );

dx = ( dxAlpha - (1-sc) * (realWidth - windowWidth)/2 )/sc;
dy = ( dyAlpha - (sc -1) * (realHeight - windowHeight)/2)/sc;
windowWidth<=600 && hideThumb();
});
function hideThumb(){}
function showThumb(){}

var s_running = false,on_scale = false,scaleSpeed = 0.05;
mcMap[id].off('pinchstart').on('pinchstart',function(e){scaled = 1;});

function pinchScale(e,delta){
    if(delta){
        e.scale = delta+1;
    }
    if(on_scale) return;
    on_scale = true;

    // console.log(sc);
    // console.log(maxSc);
    if(sc>=maxSc && e.scale>1){
        sc = maxSc;
        on_scale = false;
        return; // 已经最大，不能继续放大
    } 
    if( sc<=minSc && e.scale<1){
        sc = minSc;
        on_scale = false;
        return; // 已经最小，不能继续缩小
    } 
    sc = sc+ (e.scale>1 ? scaleSpeed : -scaleSpeed);
    setScope();

    trans(dx,dy,sc);
    on_scale = false;
}
mcMap[id].off('pinchmove').on('pinchmove',pinchScale);
$('#'+id+' .window').mousewheel(pinchScale);
window.W={pinch:pinchScale}
mcMap[id].on('pinchend',function(e){
	on_scale = false;
	scaled=2;
	setScope();
});
function trans(x,y,sct){
// console.log(x);
// console.log(y);
sct = sct || sc;
var dxAlpha = (1-sct) * (realWidth - windowWidth)/2 + x * sct;
var dyAlpha = (sct -1) * (realHeight - windowHeight)/2 + y * sct;

// 边界限制
dxAlpha = Math.max(minX, Math.min(maxX,dxAlpha) );
dyAlpha = Math.max(minY, Math.min(maxY,dyAlpha) );
var real = $('#'+id+' .real')[0];
real.style.msTransform=real.style.oTransform="translate("+dxAlpha+"px,"+dyAlpha+"px)";
real.style.transform = real.style.webkitTransform=real.style.mozTransform=
"translate3d("+dxAlpha+"px,"+dyAlpha+"px, 0) scale3d("+sct+","+sct+","+sct+")";
}
dx = 310;
dy = -390;
// if(!isIE9){sc = minSc;} // 非IE9 支持缩放
sc = minSc;
setScope();
trans(dx,dy); // 切换默认显示点
}
export {newsilen}